import { onMounted, ref } from 'vue';
import { getSysFile } from '@/apis/common/common';
import { SystemFileFileCodeEnum, SystemFileGroupCodeEnum } from '@/constants/enum';

/**
 * 模版文件下载
 * @param fileCode
 * @param groupCode
 * @returns
 */
export const useTemplateFile = (
  fileCode: keyof typeof SystemFileFileCodeEnum,
  groupCode: keyof typeof SystemFileGroupCodeEnum
) => {
  // 模型下载
  const templateUrl = ref();
  async function getTemplateFile() {
    try {
      const res = await getSysFile({
        fileCode: fileCode,
        groupCode: groupCode
      });
      templateUrl.value = res.fileUrl;
    } catch (error) {
      console.log('error', error);
    }
  }

  function handleDownloadTemplate() {
    templateUrl.value && window.open(templateUrl.value);
  }

  onMounted(() => {
    getTemplateFile();
  });

  return {
    templateUrl,
    handleDownloadTemplate
  };
};
